package com.hsj.common.system.controller;

import com.cjxch.supermybatis.base.bean.Pager;
import com.hsj.common.system.convert.SysRoleConvert;
import com.hsj.common.system.entity.SysRoleDo;
import com.hsj.common.system.service.ISysRoleService;
import com.hsj.common.system.vo.request.ReqSysRoleCreateVo;
import com.hsj.common.system.vo.request.ReqSysRoleUpdateVo;
import com.hsj.common.system.vo.request.ReqSysRolePageQueryVo;
import com.hsj.common.system.vo.response.RespSysRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.io.Serializable;


/**
 * 角色 Controller
 * @Author: Czy
 * @Date: 2024-01-23 23:03:26
 * @Email: 314170122@qq.com
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/sysRole")
@Slf4j
public class SysRoleController {

    private final ISysRoleService sysRoleService;
    private final SysRoleConvert sysRoleConvert;

    public SysRoleController(ISysRoleService sysRoleService, SysRoleConvert sysRoleConvert) {
        this.sysRoleService = sysRoleService;
        this.sysRoleConvert = sysRoleConvert;
    }

    @PostMapping("createSysRole")
    @ApiOperation(value = "创建角色", notes = "创建角色", httpMethod = "POST")
    public Serializable createSysRole(@Valid @RequestBody ReqSysRoleCreateVo vo){
        return sysRoleService.insert(sysRoleConvert.voToDo(vo));
    }

    @PostMapping("updateSysRole")
    @ApiOperation("修改角色")
    public Serializable updateSysRole(@Valid @RequestBody ReqSysRoleUpdateVo vo){
        return sysRoleService.update(sysRoleConvert.voToDo(vo));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation("删除角色")
    public Boolean delete(@PathVariable("id")String id){
        return sysRoleService.delete(id) == 1L;
    }

    @PostMapping("/pageList")
    @ApiOperation("分页查询角色")
    public Pager<RespSysRoleVo> pageList(@Valid @RequestBody ReqSysRolePageQueryVo vo){
        Pager<SysRoleDo> pager = new Pager<>();
        pager.setPageNumber(vo.getPageNumber());
        pager.setPageSize(vo.getPageSize());
        pager = sysRoleService.getPager(pager, vo.getSmCriteria());
        pager.setResult(sysRoleConvert.doToVo(pager.getResult()));
        Pager<RespSysRoleVo> retPager = new Pager<>();
        BeanUtils.copyProperties(pager, retPager);
        return retPager;
    }

    @GetMapping("/get/{id}")
    @ApiOperation("获取角色信息")
    public RespSysRoleVo get(@PathVariable("id")String id) {
        return sysRoleConvert.doToVo(sysRoleService.get(id));
    }
}
